﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Interfaces;
using CHATCENTRAL;
using ChatInterfaces;
using CHATCENTRALVIEWER;
using CHATCENTRAL.Interfaces.Entities;
using System.ServiceModel;
using Chat;
using CHATCENTRAL.Interfaces.Mappers;
using CHATCENTRAL.MAPPERS;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form, IChatCentralViewer
    {
        private ServiceHost host;
        private ChatServer cs;

        public Form1()
        {
            StartUp.GetInst().view = this;
            cs = new ChatServer(this);
            host = new ServiceHost(cs);
            host.Open();
            InitializeComponent();
            logs("Service Started");
        }

        #region IChatCentralViewer Members

        public void logTemas(IEnumerable<ITema> temas)
        {
            listBoxUsers.DataSource = temas;
            listBoxUsers.DisplayMember = "subject";
        }

        public void logUsers(IEnumerable<IClient> users)
        {
            checkedListBoxClientes.Items.Clear();

            foreach (IClient user in users)
	        {
                if(user.IsOnline)
                    checkedListBoxClientes.Items.Add(user.username, true);
                else
                    checkedListBoxClientes.Items.Add(user.username, false);
	        }

            btnStartUp.Enabled = false;
        }

        public void logUsersTemas(IEnumerable<ITema_User> usersTemas)
        {
            foreach (IClient c in usersTemas.Select(u => u.user).Distinct())
            {
                TreeNode senior = treeViewTemasUsers.Nodes.Add(c.username);
                foreach (ITema tema in usersTemas.Where(e => e.user.username == c.username).Select(y => y.tema))
                {
                    senior.Nodes.Add(tema.subject);
                }
                
            }
        }

        public void logs(string log)
        {
            //MessageBox.Show("W");
            textBoxLogs.Text = log;
        }

        #endregion

        private void btnStartUp_Click(object sender, EventArgs e)
        {
            StartUp.GetInst().SetStartUp();
        }
    }
}
